$(document).ready(
	function() {
	
		// TODO : make messages table
		$('#site_save_settings').bind('click', function() {
			oSaveParams = {
				iSiteId : oSite.iSiteId,
				sSiteName : $('#site_name').val(),
				sSiteActive : (($('#site_active').attr('checked') === true) ? 'Y' : 'N')
			};
			
			
			if (val_is_empty(oSaveParams.sSiteName) === true) {
				$.prompt('<?php echo addslashes($aMessages['admin_sites_no_name']); ?>');
				return;
			}

			
			$.post('/admin/sites_conf_ajax/save_site', oSaveParams, function(data) {
				if (data.sUpdated) {
					if (oSaveParams.iSiteId === 0) {
						$.prompt('<?php echo addslashes($aMessages['admin_sites_created']); ?>');
					} else {
						$.prompt('<?php echo addslashes($aMessages['admin_sites_updated']); ?>');
					}
				}
			}, 'json');
				
		});
		
		
		
		$('#site_domain_add').bind('click', function() {
			create_overlay(null, function(sId) {
				$.post('/admin/sites_conf_ajax/get_add_domain_form', { iSiteId : oSite.iSiteId }, function(data) {
					$('#' + sId).next().replaceWith(data.sContent);
				}, 'json');
			});
		});
		
		
		
		$('#site_language_add').bind('click', function() {
			create_overlay(null, function(sId) {
				var oImg = document.createElement('img');
				
				$(oImg).attr('src', '/assets/images/<?php echo $this->sPlatform; ?>/admin/loading/horizontal_shiny.gif').css({
					position: 'absolute',
					top: '50%',
					left: '50%',
					marginTop: '-12px',
					marginLeft: '-80px',
					zIndex: 201
				}).appendTo(document.body);
				
				$.post('/admin/sites_conf_ajax/get_add_languages', { iSiteId : oSite.iSiteId }, function(data) {
					$(oImg).replaceWith(data.sContent);
				}, 'json');
			});
		});
		
		
		
		$('.domain_remove').live('click', function() {
			var oObj = $(this);
			var iDomainId = $(oObj).parent().attr('id').replace('site_domain_', '');
			var sDomainText = $(oObj).prev().html();

			var sMessage = 'Are you sure you want to remove ' + sDomainText + ' from this site?';
						 
			if (confirm(sMessage)) {
				$.post('/admin/sites_conf_ajax/remove_domain_from_site', { iDomainId : iDomainId }, function(data) {
					$.prompt(data.sMessage);
					
					if (data.sRemoved === 'Y') {
						$(oObj).parent().slideUp(500, function() {
							$(this).remove();
							
							if ($('.site_language_cont').length > 0) {
								if ($('.site_language_cont:last').hasClass('site_domain_cont_nm') === false) {
									$('.site_domain_cont:last').addClass('site_domain_cont_nm');
								}
							}
						});
					}
					

				}, 'json');
			 }
		});
	
	
		$('.language_remove').live('click', function() {
			var oObj = $(this);
			var iSiteLangId = $(oObj).parent().attr('id').replace('site_lang_id_', '');
			var sLangText = $(oObj).prev().html();
			
			var sMessage = 'Are you sure you want to remove ' + sLangText + ' from this site?';
						 
			if ($('.language_remove').length === 1) {
				sMessage = 'This is the only language attacted to this site. If you remove it, the site will fail to work!\n\n' + sMessage;
			}
			 
			if (confirm(sMessage)) {
				$.post('/admin/sites_conf_ajax/remove_language_from_site', { iSiteLangId : iSiteLangId }, function(data) {
					$.prompt(data.sMessage);
					
					if (data.sRemoved === 'Y') {
						$(oObj).parent().slideUp(500, function() {
							$(this).remove();
							
							if ($('.site_language_cont').length > 0) {
								if ($('.site_language_cont:last').hasClass('site_language_cont_nm') === false) {
									$('.site_language_cont:last').addClass('site_language_cont_nm');
								}
							}
						});
					}
					

				}, 'json');
			 }
			 
		});
		
		
		$('#site_enter_domain_button').live('click', function(e) {
			var sSiteDomain = $('#site_enter_domain').val();
			
			if (val_is_empty(sSiteDomain) === true) {
				$.prompt('<?php echo addslashes($aMessages['admin_sites_add_domain_no_value']); ?>');
				return;
			}
			
			$.post('/admin/sites_conf_ajax/confirm_add_domain', { iSiteId : oSite.iSiteId, sSiteDomain : sSiteDomain }, function(data) {
				if ($('.site_domain_cont').length === 0) {
					$('#site_domain_domains_cont').html(data.sContent);
				} else {
					$('.site_domain_cont:last').removeClass('site_domain_cont_nm').after(data.sContent)
				}
				
				$('#site_add_domain_form').fadeOut(250, function() {
					$(this).remove();
					$('#<?php echo $this->config->item('site_accr'); ?>_overlay').remove();
				});
			}, 'json');
		});
		
		
		$('#site_add_languages_button').live('click', function(e) {
			if ($(':checkbox[name="site_add_language[]"]:checked').length === 0) {
				$.prompt('<?php echo addslashes($aMessages['admin_sites_add_language_none_selected']); ?>');
				return;
			}
			
			
			var aLangIds = [];
			var aLangTexts = [];

			
			$(':checkbox[name="site_add_language[]"]:checked').each(function(nr) {
				aLangIds[nr] = $(this).val();
				aLangTexts[nr] = $(this).next().html();
			});
			
			var sLangIds = aLangIds.join('::');
			var sLangTexts = aLangTexts.join('::');
			
			$.post('/admin/sites_conf_ajax/confirm_add_languages', { iSiteId : oSite.iSiteId, sLangIds : sLangIds, sLangTexts : sLangTexts }, function(data) {
				if ($('.site_language_cont').length === 0) {
					$('#site_languages_languages_cont').html(data.sContent);
				} else {
					$('.site_language_cont:last').removeClass('site_language_cont_nm').after(data.sContent)
				}
				
				$('#site_add_language_form').fadeOut(250, function() {
					$(this).remove();
					$('#<?php echo $this->config->item('site_accr'); ?>_overlay').remove();
				});
			}, 'json');
			
		});
	}
);